# -*- coding: utf-8 -*-
import numpy as np
import scipy.linalg as linA # 为了激活线性代数库mkl
from PIL import Image
import cv2
import os,glob

def sim_distance(train,test):
    '''
    计算欧氏距离相似度
    :param train: 二维训练集
    :param test: 一维测试集
    :return: 该测试集到每一个训练集的欧氏距离
    '''
    return [np.linalg.norm(i - test) for i in train]

picture_path = '1\\'
array_list = []
for name in glob.glob(picture_path+'*.bmp'):
    # img = cv2.imread(name,cv2.IMREAD_GRAYSCALE)
    img = cv2.imread(name)
    rows,cols,dims = img.shape
    img = np.reshape(img,(rows*cols*dims))
    array_list.append(list(img))
#     # 读取每张图片并生成灰度（0-255）的一维序列 1*120000
#     img = Image.open(name)
#     # img_binary = img.convert('1') 二值化
#     img_grey = img.convert('L') # 灰度化
#     array_list.append(np.array(img_grey).reshape((1,10304)))



mat = np.vstack((array_list))
print(mat.shape)
P = np.dot(mat,mat.T)  
v,d = np.linalg.eig(P)  
d= np.dot(mat.T,d)  
train = np.dot(d.T,mat.T)  

print(train)
# 开始测试
test_pic = np.array(cv2.imread('1\\2.BMP')).reshape((1,30912))
result = sim_distance(train.transpose(),np.dot(test_pic,d))
print (result)
# test_pic = np.array(Image.open('e.jpg').convert('L')).reshape((1,10304))
# result = sim_distance(train.transpose(),np.dot(test_pic,d))
# print (result)